package com.yljk.yljkproject.modelcallback.live;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.alibaba.fastjson.JSON;
import com.yilijk.base.network.https.HttpUtils;
import com.yilijk.base.network.https.interceptor.httplog.HttpLogInterceptor;
import com.yilijk.base.utils.ALog;
import com.yilijk.base.utils.DevicesUtils;
import com.yilijk.base.utils.SharedPreferencesUtils;
import com.yljk.mcbase.provider.LiveService;
import com.yljk.mcconfig.constants.MCARouter;
import com.yljk.mcconfig.constants.MCCommonKeys;
import com.yljk.servicemanager.constants.ReactApi;
import com.yljk.servicemanager.constants.RouterApi;
import com.yljk.servicemanager.interfaceapi.MineBackApi;
import com.yljk.yljkproject.utils.ReactUtils;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import okhttp3.Interceptor;

/**
 * @author : cenjunlei
 * @date : 2021/8/15 3:04 下午
 * @desc : Live相关服务
 */
@Route(path = MCARouter.LIVE_SERVICE)
public class LiveServiceImpl implements LiveService {
    private Context mContext;

    @Override
    public void init(Context context) {
        mContext = context;
    }

    @Override
    public void onCancelLogout(Activity activity) {
        MineBackApi mineBackApi = (MineBackApi) ARouter.getInstance()
                .build(RouterApi.MINEBACKIMPL)
                .navigation();
        mineBackApi.onCancelLogout(activity, activity);
    }

    @Override
    public String getBaseUrl() {
        return HttpUtils.getUrl();
    }

    @Override
    public void showApprovePage() {
        Bundle bundle = new Bundle();
        bundle.putString("certificationType","livecert");
        ReactUtils.INSTANCE.push(mContext, ReactApi.LOGIN_DOCTORBUNDLE, ReactApi.Login.AutonymApprovePage,bundle);
    }

    @Override
    public Interceptor[] getInterceptors() {
        return new Interceptor[]{new HttpLogInterceptor()};
    }

    @Override
    public HashMap<String, Object> getDeviceInfoMap() {
        return DevicesUtils.addDeviceInfo(new HashMap<>());
    }

    @Override
    public String getString(String tag) {
        String result = null;
        switch (tag){
            case MCCommonKeys.Service.KEY_QUERY_DOCTOR_STATUS:
                result = SharedPreferencesUtils.getInstance().getString("queryDoctorStatus", null);//本地初审状态
                break;
            case MCCommonKeys.Service.KEY_PRELIMINARY_STATUS_UPDATE:
                result = SharedPreferencesUtils.getInstance().getString("queryDoctorStatus", null);//本地初审状态
                break;
        }
        return result;
    }

    @Override
    public void openRnPage(String pluginId, String compontName, String paramsJson) {
        ALog.i(paramsJson);
        if (TextUtils.isEmpty(paramsJson)){
            ReactUtils.INSTANCE.push(mContext,pluginId,compontName);
            return;
        }
        Bundle bundle = new Bundle();
        Map<String, Object> extMap = (Map<String, Object>) JSON.parse(paramsJson);
        Set<Map.Entry<String, Object>> entries = extMap.entrySet();
        Iterator<Map.Entry<String, Object>> iterator = entries.iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Object> next = iterator.next();
            bundle.putString(next.getKey(),next.getValue().toString());
        }
        ReactUtils.INSTANCE.push(mContext,pluginId,compontName,bundle);
    }


}
